//
//  DISAorderGoodLeftViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/10.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "DISAorderGoodLeftViewController.h"
#import "GoodListModel.h"
//#import "CSgoodsViewController.h"
#import "HomeGoodsCell.h"
#import "DISLectRightViewController.h"


@interface DISAorderGoodLeftViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *modelArr;
@property(nonatomic,copy)NSString *type;
@property(nonatomic,assign) NSInteger page;

@property(nonatomic,strong)UITextField *mytf;


@end

@implementation DISAorderGoodLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.modelArr = [NSMutableArray arrayWithCapacity:1];
    [self.view addSubview:self.collectionView];
    [self.collectionView reloadData];
    [self requestGoods];
    KMyLog(@"type+++==%@",_type);
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhixialaaction:) name:@"tongzhseleproduce" object:nil];

    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    
}
-(void)tongzhixialaaction:(NSNotification *)no{
    _page = 1;
    NSString *idstr = [no.userInfo objectForKey:@"keyidstr"];
    
    _type =idstr;

    [self requestGoods];
    
  
    
    
    
}

- (void)requestGoods {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"type"] = _type;
    NSString *pp = [NSString stringWithFormat:@"%ld",(long)_page];
    param[@"pageNum"] = pp;
    if (_page == 1) {
        [self.modelArr removeAllObjects];
        
    }
    kWeakSelf;
    
    [NetWorkTool POST:getPlatProList param:param success:^(id dic) {

        self.modelArr = [GoodListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        
        [weakSelf.collectionView.mj_footer endRefreshing];
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView reloadData];
        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView reloadData];

    } fail:^(NSError *error) {
        [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
        [weakSelf.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView reloadData];
    } needUser:NO];
    
    
    
}

#pragma mark - CollectionView -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _modelArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGoodsCell" forIndexPath:indexPath];
    
    GoodListModel *model = [_modelArr safeObjectAtIndex:indexPath.row];
    [cell refreshs:model];
    return cell;
}




- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath {
    GoodListModel *model = [_modelArr safeObjectAtIndex:indexPath.row];
    DISLectRightViewController *vc = [DISLectRightViewController new];
    vc.goodsID = model.Id;    
    [self.navigationController pushViewController:vc animated:YES];
    
}



-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(KWIDTH/2 , 260);
        //        layout.headerReferenceSize = CGSizeMake(KWIDTH, 204);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT-kNaviHeight-60) collectionViewLayout:layout];
        adjustInset(_collectionView);
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor =RGBA(237, 237, 237, 1);
        
        [_collectionView registerNib:[UINib nibWithNibName:@"HomeGoodsCell" bundle:nil] forCellWithReuseIdentifier:@"HomeGoodsCell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self requestGoods];
        }];
        
        _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page += 1;
            [self requestGoods];
        }];
        
        
        [_collectionView reloadData];
        
    }
    return _collectionView;
}

@end
